from MyService import MyService
import tkinter
from tkinter.filedialog import askopenfilenames
from dbUtil import DBUtil as Mydb
class playWindow:

    def __init__(self,myService):
        self.myService = myService

    def showWindow(self):
        tk = tkinter.Tk()

        #添加按钮
        PlayButton = tkinter.Button(tk, text='播放')
        ImpButton = tkinter.Button(tk,text = '导入音乐')
        DeleteButton = tkinter.Button(tk,text = '删除')
        #按钮布局
        PlayButton.grid(row= 0,column =0,padx=5,pady=5)
        ImpButton.grid(row= 0,column =2,padx=5,pady=5)
        DeleteButton.grid(row= 0,column =4,padx=5,pady=5)

        #添加列表
        listbox = tkinter.Listbox()
        listbox.grid(row =1 ,column=0,padx = 5,pady=5,columnspan=9)

        #给按钮绑定事件
        ImpButton.bind('<ButtonRelease-1>',self.impMusic)
        PlayButton.bind('<ButtonRelease-1>',self.playMusic)
        DeleteButton.bind('<ButtonRelease-1>',self.deleteMusic)
        # 显示窗口
        tk.mainloop()

    #给用户添加音乐
    def impMusic(self,even):
        print('点击')
        #打开本地磁盘 返回多个列表
        files = askopenfilenames(filetype=(["mp3", "*.mp3"],))
        #导入音乐，将选择的音乐保存到数据库
        self.myService.addMusic(files)

    #查询当前用户音乐列表
    def flushList(self):
        pass

    #进行播放音乐
    def playMusic(self,even):
        print('播放')

    # 进行删除音乐
    def deleteMusic(self,even):
        print('删除')

if __name__ == '__main__':
    username = input("请输入用户名：")
    password = input("请输入密码：")
    #创建实例myservice对象
    myservice = MyService()
    '''user = Mydb()
    sql = "select * from t_user"
    AllResult  = user.query_all(sql)
    print(type(AllResult))
    print(AllResult)
    print(AllResult[len(AllResult)-1][0])
    if AllResult:
        for i in AllResult:
            print(i)
    else:
        print('none')'''
    if myservice.login(username,password):
        playWindow = playWindow(myservice)
        playWindow.showWindow()
    else:
        print('登录失败')
